// 日期选择
import rxFixed from '../../others/fixed'
import Datepikcer from './datepicker.vue'
import {format as dateFormat} from '@rxTools/ext/date'

function pickerDate({
  el,//容器
  event,//触发的事件
  placement,//方位
  value,
  type,
  limit_min,//限制最小日期
  limit_max,//限制最大日期
}){

 return  rxFixed({
    el,
    event,
    placement,
    propsData:{
      value,
      type,
      limit_min,
      limit_max
    }
  },
    Datepikcer).then((date)=>{
      return dateFormat(date,'yyyy.MM.dd')
    });
}
const format = 'yyyy-MM-dd';
/** 根据输入的日期获取最近的时间 */
pickerDate.getDateForDay = function (day) {
  let date = new Date(),
    targetDate = date.getTime() + 1000 * 60 * 60 * 24 * day;
  return dateFormat(targetDate, format);

}
/** 近一周 */
pickerDate.getNearlyWeek = function () {
  return this.getDateForDay(-6);
}
/** 近一月 */
pickerDate.getNearlyMonth = function () {
  return this.getDateForDay(-30);
}
/** 本年 */
pickerDate.getCurrentYear = function () {
  let date = new Date();
  return getCurrentDate(date.getFullYear());
}
/** 本季 */
pickerDate.getCurrentQuarter = function () {
  let date = new Date(); 
  return getCurrentDate(date.getFullYear(), getQuarterStartMonth(), true);
}
/** 本月 */
pickerDate.getCurrentMonth = function () {
  let date = new Date();
  return getCurrentDate(date.getFullYear(), date.getMonth());
}

/** 获取某年某月的开始时间与结束时间， quarter获取季度 */
function getCurrentDate(year, date, quarter = 0) {
  return {
    sDate: dateFormat(new Date(year, date || 0, 1), format),
    eDate: dateFormat(new Date(year, date + 1 + quarter * 2 || 12, 0), format)
  }
}
/** 获得本季度的开始月份 */
function getQuarterStartMonth() {
  var quarterStartMonth = 0, month = new Date().getMonth();
  if (month < 3) {
    quarterStartMonth = 0;
  }
  if (2 < month && month < 6) {
    quarterStartMonth = 3;
  }
  if (5 < month && month < 9) {
    quarterStartMonth = 6;
  }
  if (month > 8) {
    quarterStartMonth = 9;
  }
  return quarterStartMonth;
}
export default pickerDate;
